package com.gxuwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.gxuwz.medical.dao.LoginDao;
import com.gxuwz.medical.domain.user.User;
/**
 * 登录验证控制模块
 * @author 演示
 *
 */

public class LoginServlet extends BaseServlet {

	private static final Logger logger = LogManager.getLogger(LoginServlet.class);
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		login(request, response);
		
	}
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String userid = request.getParameter("userid");
		String pwd = request.getParameter("pwd");
		User user = new User();
		LoginDao loginDao = new LoginDao();
		HttpSession session = request.getSession();
		
		try{
			user = loginDao.login(userid);
			
		}catch(Exception e){
			e.printStackTrace();
			
		}
		if(user!=null){
			if(user.getUserid().equals(userid)&&user.getPwd().equals(pwd)){
				response.sendRedirect(request.getContextPath()
						+ "/index.jsp");
			}else{
				request.getRequestDispatcher("/fail.jsp").forward(request,
						response);
				
			}
		}else{
			request.getRequestDispatcher("/fail.jsp").forward(request,
					response);
		}
		   
		
	}


}
